package cn.iocoder.yudao.module.vehicle.controller.admin.fuelcardapplication;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import cn.iocoder.yudao.module.vehicle.controller.admin.fuelcardapplication.vo.*;
import cn.iocoder.yudao.module.vehicle.dal.dataobject.fuelcardapplication.FuelcardApplicationDO;
import cn.iocoder.yudao.module.vehicle.service.fuelcardapplication.FuelcardApplicationService;

@Tag(name = "管理后台 - 油卡申请记录")
@RestController
@RequestMapping("/vehicle/fuelcard-application")
@Validated
public class FuelcardApplicationController {

    @Resource
    private FuelcardApplicationService fuelcardApplicationService;

    @PostMapping("/create")
    @Operation(summary = "创建油卡申请记录")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:create')")
    public CommonResult<Long> createFuelcardApplication(@Valid @RequestBody FuelcardApplicationSaveReqVO createReqVO) {
        return success(fuelcardApplicationService.createFuelcardApplication(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新油卡申请记录")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:update')")
    public CommonResult<Boolean> updateFuelcardApplication(@Valid @RequestBody FuelcardApplicationSaveReqVO updateReqVO) {
        fuelcardApplicationService.updateFuelcardApplication(updateReqVO);
        return success(true);
    }

    @PutMapping("/submit")
    @Operation(summary = "提交油卡申请审批")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:update')")
    public CommonResult<Boolean> submitApplicaton(@RequestParam("id") Long id) {
        fuelcardApplicationService.submitApplicaton(id, getLoginUserId());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除油卡申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:delete')")
    public CommonResult<Boolean> deleteFuelcardApplication(@RequestParam("id") Long id) {
        fuelcardApplicationService.deleteFuelcardApplication(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除油卡申请记录")
                @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:delete')")
    public CommonResult<Boolean> deleteFuelcardApplicationList(@RequestParam("ids") List<Long> ids) {
        fuelcardApplicationService.deleteFuelcardApplicationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得油卡申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:query')")
    public CommonResult<FuelcardApplicationRespVO> getFuelcardApplication(@RequestParam("id") Long id) {
        FuelcardApplicationDO fuelcardApplication = fuelcardApplicationService.getFuelcardApplication(id);
        return success(BeanUtils.toBean(fuelcardApplication, FuelcardApplicationRespVO.class));
    }


    @GetMapping("/getbyprocid")
    @Operation(summary = "根据流程编号获取油卡申请记录")
    @Parameter(name = "id", description = "流程编号", required = true, example = "556ee260-a89c-11f0-9701-00155d4c91a7")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:query')")
    public CommonResult<FuelcardApplicationRespVO> getFuelcardApplicationByProcId(@RequestParam("id") String id) {
        FuelcardApplicationDO fuelcardApplication = fuelcardApplicationService.getFuelcardApplicationByProcId(id);
        return success(BeanUtils.toBean(fuelcardApplication, FuelcardApplicationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得油卡申请记录分页")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:query')")
    public CommonResult<PageResult<FuelcardApplicationRespVO>> getFuelcardApplicationPage(@Valid FuelcardApplicationPageReqVO pageReqVO) {
        PageResult<FuelcardApplicationDO> pageResult = fuelcardApplicationService.getFuelcardApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FuelcardApplicationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出油卡申请记录 Excel")
    @PreAuthorize("@ss.hasPermission('vehicle:fuelcard-application:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFuelcardApplicationExcel(@Valid FuelcardApplicationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FuelcardApplicationDO> list = fuelcardApplicationService.getFuelcardApplicationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "油卡申请记录.xls", "数据", FuelcardApplicationRespVO.class,
                        BeanUtils.toBean(list, FuelcardApplicationRespVO.class));
    }

}